<?php

class Admin extends _Controller
{
    private $users;
    private $url;
    private $odb;

    public function __construct()
    {
        $this->importHelper('users');
        $this->users = new Users();
        if(!$this->users->IsAdmin())
        {
            if(isset($_POST['login']) && isset($_POST['pass']))
            {
                $id = $this->users->CheckAdminLogin(array('UserName' => $_POST['login'], 'Password' => $_POST['pass']));
                $this->users->LoginAdmin(array('ID' => $id));
                header('location:' . $_SERVER['HTTP_REFERER']);
                return;
            }
            $this->showLogin();
        }

        $this->url = getModule('Url');
        $this->odb = getModule('Odb');
    }

    private function showLogin()
    {
        $this->parseTemplate('admin-header');
        $this->parseTemplate('admin-login');
        $this->parseTemplate('admin-footer');
        die;
    }

    public function get()
    {
        $this->parseTemplate('admin-header');
        $this->parseTemplate('admin-menu');

        $page = $this->url->get(1);

        switch($page)
        {
            case '':
                $this->home();
                break;
            case 'categories':
                $this->categories();
                break;
            case 'about':
                $this->about();
                break;
            case 'contact':
                $this->contact();
                break;
            case 'users':
                $this->users();
                break;
            case 'clients':
                $this->clients();
                break;
            case 'logout':
                $this->logout();
        }

        $this->parseTemplate('admin-footer');
    }

    public function post()
    {
    }

    private function home()
    {
        $this->parseTemplate('admin-home');
    }

    private function categories()
    {
        $this->parseTemplate('admin-categories');
    }

    private function about()
    {
        $this->parseTemplate('admin-about');
    }

    private function contact()
    {
        $this->parseTemplate('admin-contact');
    }

    private function users()
    {
        $this->parseTemplate('admin-users');
    }

    private function clients()
    {
        $this->parseTemplate('admin-clients');
    }

    private function logout()
    {
        $this->users->LogoutUser();
        header('location:' . URL . '/admin/');
    }
}